package day05;

import java.util.Arrays;

/*
@Author: 王少杰
@Date: 2025/1/4
@Time: 9:00
@FileName: Demo01.java
@描述:
String 字符串常用操作

*/
public class Demo01 {
    public static void main(String[] args) {
        String a = "Hello World"; //字符串是不可变类型
        System.out.println(a.length());
        System.out.println(a.toLowerCase());//转成小写
        System.out.println(a.toUpperCase());//转成大写
        System.out.println(a.charAt(0));//a[0]


        String b = "HelloWorld";
        int x = a.compareTo(b); //W-w
//        System.out.println(a.compareTo(b));
        System.out.println("返回第一个不相等字符的字符差:" + x); //-55
        System.out.println(b.compareTo(a)); //w-W  55

        String c = new String("Hello World"); //构造方法可以省略
        System.out.println(a.compareTo(c));//字符串相等，返回0

        //字符串判等，不能使用==，==比较的是内存地址
        System.out.println(a == c);

        //equals 先比较内存地址，内存地址不一样时，比较字符串的内容
        System.out.println(a.equals(c));
        System.out.println(a.hashCode());//哈希码
        System.out.println(c.hashCode());
        System.out.println(System.identityHashCode(a));
        System.out.println(System.identityHashCode(c));

        System.out.println(a.equalsIgnoreCase(b));//忽略大小写

        //substring
        System.out.println(a.substring(0, 5));//包含开始，不包含结束
        System.out.println(a.substring(5));//不写结束。默认到最后

        //字符/子串在字符串中的位置
        //indexOf  lastIndexOf
        String path = "LearnJava/src/day04/Demo02.java";
        System.out.println(path.indexOf("day04")); //  \\是一个字符
        System.out.println(path.lastIndexOf("."));
        System.out.println(path.substring(path.lastIndexOf("."))); //java 取出后缀

        //格式化 format
        /*
        %s:字符串
        %d:整数
        %f:浮点数
        %.2f:小数点后两位
        %%:表示一个百分号
         */
        double f = 1.23411345 + 5.41216541;
        System.out.println(f);
        String xx = String.format("这个数字为%s", f);
        System.out.println(xx);

        xx = String.format("数字为: %f %.2f %d", f, f, (int) f);
        System.out.println(xx);
        System.out.println(String.format("女生的占比为: %d%%", 50));

        //split join

        String yy = "1.2.3.4.5.6.7";
        String[] temp = yy.split("\\.");//split成员方法
        System.out.println(Arrays.toString(yy.split("\\.")));

        String zz = String.join("+", temp);//join 静态方法
        System.out.println(zz);

        //
        String index = "Hello World";
        StringBuilder result = new StringBuilder();

        for (int i = 0; i < index.length(); i++) {
            char c1 = index.charAt(i);
            if (Character.isLowerCase(c1)) {
                result.append(Character.toUpperCase(c1));
            } else if (Character.isUpperCase(c1)) {
                result.append(Character.toLowerCase(c1));
            } else {
                result.append(c);
            }
        }
        System.out.println(result);
    }
}
